package cfmhw;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class CfmHwServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String beginHtml = "<html><body><h2>";
		String endHtml = "</h2></body></html>";
		String novaLínia = "<br></br>";
		String llibre, entrada, valor;
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user !=null) {
			Date date = new Date();
			llibre = req.getParameter("llibre");
			entrada = req.getParameter("entrada");
			valor = req.getParameter("valor");
			resp.setContentType("text/html");
			resp.getWriter().println(beginHtml);
			resp.getWriter().println("Versió v010. " + 
					DateFormat.getDateInstance().format(date) + " - " + DateFormat.getTimeInstance().format(date)	 
				+ novaLínia + "Salutacions " + 
					user + novaLínia + "Això és codi Java 6 executat al cloud de Google." );
			resp.getWriter().println("<br></br><br>llibre: " + llibre + "</br>");
			resp.getWriter().println("<br>entrada: " + entrada + "</br>");
			resp.getWriter().println("<br>valor: " + valor + "</br>");
			resp.getWriter().println(beginHtml);
			resp.getWriter().println(beginHtml);
			resp.getWriter().println(endHtml);
		}else{
			resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));			
		}
		
	}
}
